package com.org.Bibliotech;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MyUnit {

    private static Library library;

    public static void main(String[] args) {

        MyUnit myUnit = new MyUnit();
        myUnit.createLibrary();

        System.out.print("Please enter your name: ");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

       try{
           System.out.println("Welcome to Biblioteca, " + reader.readLine() + '!');
       } catch(IOException e){
           System.out.println("There was a problem reading your name. Please try again");
           myUnit.main(null);
       }


        myUnit.renderMainMenu();


    }

    public void createLibrary(Library library){
        this.library = library;
    }

    public void createLibrary(){

        Book HeadFirstJava = new Book("Head First Java", "Kathy Sierra & Bert Bates", 2003);
        Book AnnaKarenina = new Book("Anna Karenina", "Leo Tolstoy", 1877);
        ArrayList<Book> books = new ArrayList<Book>();

        books.add(HeadFirstJava);
        books.add(AnnaKarenina);

        library = new Library(books);
    }

    public void renderMainMenu() {

        System.out.println("Main menu\n");

        System.out.println("<======================>");
        System.out.println("Please select a number");

        System.out.println("0. Exit");
        System.out.println("1. See Available Books");
        System.out.println("2. Return a Book");

        System.out.println("<======================>");

        System.out.println();

        String command = getCommand();
        executeCommand(command);


    }

    public String getCommand(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


        System.out.print("Please enter your choice: ");
        String command = null;

        try{
            command = reader.readLine();
        } catch(IOException e){
            System.out.println("Couldn't read the command.");
            e.printStackTrace();
        }

        System.out.println();
        return command;
    }

    public void executeCommand(String _command){

        int command = -1;

        command = Integer.parseInt(_command);

        switch(command){

            case 0: prepExit(); break;

            case 1:

                listBooks();
                System.out.println("Enter the number of the book you'd like to check out or 0 to leave");
                int bookToGet = Integer.parseInt(getCommand());

                if(bookToGet == 0)
                    prepExit();

                else
                    checkoutBook(bookToGet);

                break;

            case 2: returnBook();
                     break;


            default: System.out.println("Select a valid option!");
                     executeCommand(getCommand());

        }
    }

    public void prepExit(){

        System.out.println("Are you sure you would like to exit? Y/N");
        char command = getCommand().toUpperCase().charAt(0);

        if(command == 'Y')
            System.exit(0);

        else renderMainMenu();

    }


    public void listBooks(){
        ArrayList<Book> books = library.getAvailableBooks();

        if(books.size() == 0){
            System.out.println("All of our books our checked out. ");
            renderMainMenu();
            return;
        }


        int bookNumber = 1;
        for(Book book : books){

            System.out.println(bookNumber + ".");
            System.out.println(book.getDetails());
            System.out.println();

            bookNumber++;

        }
    }



    public void returnBook(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

         String title = null;
        String author = null;
         int publicationYear = -1;

         try {
             System.out.print("Please enter the title of the book: ");
              title = reader.readLine();

             System.out.print("Please enter the author(s) of the book: ");
              author = reader.readLine();

             System.out.print("Please enter year the book was published of the book: ");
              publicationYear = Integer.parseInt(reader.readLine());
         } catch(IOException e){
             System.out.println("Trouble creating the book. Please try again");
             returnBook();
         }

        if(!title.equals(null) && !author.equals(null) && publicationYear != -1)
            System.out.println(returnBook(new Book(title, author, publicationYear)));

        else System.out.println("Something's gone wrong.");
         renderMainMenu();
    }

    public String returnBook(Book book){

        if(library.returnBook(book))
            return "Thank you for returning the book.";

        else return "That is not a valid book to return.";
    }


    public void checkoutBook(int bookToGet){


        try {
            Book bookToCheckOut = (Book)library.getAvailableBooks().get(bookToGet-1);

            System.out.println(checkOut(bookToCheckOut));
            System.out.println();

            renderMainMenu();
        }catch(IndexOutOfBoundsException e){
            System.out.println("That wasn't a valid book! Please enter a valid book!");

            int newBook = Integer.parseInt(getCommand());
            checkoutBook(newBook);
        }


    }

    public String checkOut(Book book){

        if(library.checkoutBook(book))
            return "Thank you! Enjoy the book";

        else return "That book is not available.";
    }


}
